﻿using Autofac;
using CSRedis;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SH.Common;
using SH.Common.CommonReturn;
using SH.Multitenant.API.Filters;
using SH.Multitenant.Application.IServices.IPermission;
using SH.Multitenant.Domain.RBAC;


namespace SH.Multitenant.API.Controllers
{
    /// <summary>
    /// API控制器-权限
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class PermissionController : ControllerBase
    {
        private readonly IPermissionServices _permissionServices;
        private readonly CSRedisClient _redis = AppService.AutofacContainer.Resolve<CSRedisClient>();
        /// <summary>
        /// 构造方法注入
        /// </summary>
        /// <param name="permissionServices"></param>
        public PermissionController(IPermissionServices permissionServices)
        {
            _permissionServices = permissionServices;
        }
        /// <summary>
        /// 根据用户获取菜单权限
        /// </summary>
        /// <param name="empId"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenusByEId(string empId, string? parentId = "Top")
        {
            var result = _permissionServices.GetMenusByEId(empId, parentId);
            ApiResult<List<MenuDTO>> apiResult = new ApiResult<List<MenuDTO>>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 获取所有的权限
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<IActionResult> GetAllPermissions()
        {
            string tenantId = _redis.Get<string>("tenantId");
            var result = _redis.HGet<List<Permission>>("allPermissionList", tenantId);
            ApiResult<List<Permission>> apiResult = new ApiResult<List<Permission>>();
            if (result != null && result.Count() > 0)
            {
                apiResult.Success(result);
                return Ok(apiResult);
            }
            result = await _permissionServices.All.ToListAsync();
            _redis.HSet("allPermissionList", tenantId, result);
            _redis.Expire("allPermissionList", 60);
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 根据地址获取当前用户当前地址的按钮权限
        /// </summary>
        /// <param name="empId"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetButtonPermissionsByPath(string? empId, string? path)
        {
            var result = await _permissionServices.GetButtonPermissionsByPathAsync(empId, path);
            ApiResult<List<Permission>> apiResult = new ApiResult<List<Permission>>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 获取所有的权限树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllMenus()
        {
            var result = _permissionServices.GetAllMenus();
            ApiResult<List<MenuDTO>> apiResult = new ApiResult<List<MenuDTO>>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 根据角色id获取角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPermissionsByRoleId(int roleId)
        {
            var result = _permissionServices.GetPermissionsByRoleId(roleId);
            ApiResult<List<Permission>> apiResult = new ApiResult<List<Permission>>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="pIds"></param>
        /// <returns></returns>
        [HttpPost]
        [LogActionFilter("Top", "修改角色权限")]
        public IActionResult UpdRolePermissions(int roleId, string? pIds)
        {
            var result = _permissionServices.UpdRolePermissions(roleId, pIds);
            ApiResult<int> apiResult = new ApiResult<int>();
            if (result > 0)
            {
                apiResult.Success();
            }
            else
            {
                apiResult.Error(error: ApiResult<int>.ErrorType.Request, "修改失败");
            }
            return Ok(apiResult);
        }
        /// <summary>
        /// 根据父级编号获取权限列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        //[HttpGet]
        //public async Task<IActionResult> GetSubPermissionsByParentId(string? parentId)
        //{
        //    ApiResult<List<string>> apiResult = new ApiResult<List<string>>();
        //    if (string.IsNullOrEmpty(parentId))
        //    {
        //        throw new ArgumentNullException(nameof(parentId));
        //        return BadRequest();
        //    }
        //    var result = await _permissionServices.GetSubPermissionsByParentId(parentId);

        //    apiResult.Success(result);
        //    return Ok(apiResult);
        //}

        /// <summary>
        /// 根据权限编号获取父级编号
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetParentIdByPId(string? pId)
        {
            var result = await _permissionServices.GetParentIdByPId(pId);
            ApiResult<string> apiResult = new ApiResult<string>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 判断该用户是否有资源对应的权限
        /// </summary>
        /// <param name="empId"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeterminesWhetherHasThisPermissionAsync(string empId, string path)
        {
            var result = await _permissionServices.DeterminesWhetherHasThisPermissionAsync(empId, path);
            ApiResult<bool> apiResult = new ApiResult<bool>();
            apiResult.Success(result);
            return Ok(apiResult);
        }

        /// <summary>
        /// 多条件分页获取权限列表
        /// </summary>
        /// <param name="pName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCondtionPagingPermissionList(string? pName, int pageSize, int pageIndex)
        {
            var result = await _permissionServices.GetCondtionPagingPermissionList(pName, pageSize, pageIndex);
            return Ok(result);
        }
    }
}
